package com.cyy.common.config.satoken;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.hutool.http.useragent.UserAgent;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyy.common.constant.CacheKey;
import com.cyy.common.constant.Constants;
import com.cyy.common.domain.OnlineUser;
import com.cyy.common.mapper.UserMapper;
import com.cyy.common.pojo.entity.User;
import com.cyy.common.utils.IpUtils;
import com.cyy.common.utils.RedisUtils;
import com.cyy.common.utils.RequestContextUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
@RequiredArgsConstructor
public class MySaTokenListener implements SaTokenListener {

    private final RedisUtils redisUtils;

    private final UserMapper userMapper;


    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {

        HttpServletRequest request = RequestContextUtils.getRequest();
        UserAgent userAgent = IpUtils.getUserAgent(request);
        String ip = IpUtils.getIpAddr(request);
        String address = IpUtils.getIp2region(ip);

        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getId, loginId)
        );

        user.setLoginIp(ip);
        user.setLoginBrowser(userAgent.getBrowser().getName());
        user.setLoginOs(userAgent.getOs().getName());
        user.setLoginSource(address);
        user.setLoginDate(LocalDateTime.now());
        user.setLoginCount(user.getLoginCount() + 1);

        // 更新用户信息
        userMapper.updateById(user);

        OnlineUser onlineUser = new OnlineUser();

        BeanUtils.copyProperties(user, onlineUser);
        onlineUser.setLogoutDate(onlineUser.getLoginDate().plusSeconds(loginModel.getCookieTimeout()));
        onlineUser.setTime(loginModel.getCookieTimeout());

        // 先
        redisUtils.set(String.join(Constants.COLON, CacheKey.USER_ONLINE, loginId.toString()), onlineUser, onlineUser.getTime());
    }

    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        redisUtils.del(String.join(Constants.COLON, CacheKey.USER_ONLINE, loginId.toString()));
    }

    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        redisUtils.del(String.join(Constants.COLON, CacheKey.USER_ONLINE, loginId.toString()));
    }

    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        redisUtils.del(String.join(Constants.COLON, CacheKey.USER_ONLINE, loginId.toString()));
    }

    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {

    }

    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {

    }

    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {

    }

    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {

    }

    @Override
    public void doCreateSession(String id) {

    }

    @Override
    public void doLogoutSession(String id) {

    }

    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {

    }
}
